package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Ioperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("添加一本书");
        System.out.println("请输入图书名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("这本书已经存在，不在进行新增..");
                System.out.println(book);
                return;
            }
        }

        System.out.println("请输入你要新增的图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增的图书的价格:");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入你要新增的图书的类型:");
        String type = scanner.nextLine();
        Book book = new Book(name, author, price, type);
        bookList.setBook(currentSize, book);
        bookList.setUsedSize(currentSize + 1);
    }
}
